<?php
/**
 * User_model - модель пользователя
 * 
 * @author Mazvv
 * @package Extenders
 */
class User_model extends Model {
	
	public function __construct(){
		parent::Model();
		$this->db->query('set names \'utf8\'');	
	}
	
	/**
	 * �������� ������ ��� ������������
	 * 
	 * @author Mazvv
	 * @param $p_userRid
	 * @return fullName
	 */
	public function GetUserFullName($p_userRid){
		$this->db->select('concat(TRIM(_employeers.f_name), \' \', TRIM(_employeers.s_name), \' \', TRIM(_employeers.l_name)) as fullName');
		$this->db->from('_users');
		$this->db->join('_employeers', '_users._employeers_rid = _employeers.rid AND _employeers.archive = 0');
		$this->db->where(array('_users.rid'=>$p_userRid, '_users.archive'=>0));
		$query = $this->db->get();
		if($query->num_rows) {
			$row = $query->row();
			return $row->fullName;
		}
		return False;
	}
	
	/**
	 * ����� ������ ������������
	 * 
	 * @author Mazvv
	 * @param $p_userRid
	 * @return boolean
	 */
	public function ChangeUserPasswd($p_userRid, $p_newPasswd){
		$nextDate = date('Y-m-d', mktime(date("H"), date("i"), date("s"), date("m")+1, date("d"), date("Y")));
		$this->db->trans_begin();
		$this->db->where(array('_users.rid'=>$p_userRid));
		$this->db->update('_users', array('_users.user_passwd'=>$p_newPasswd, '_users.chdate_passwd'=>$nextDate));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		} else {
    		$this->db->trans_commit();
		}
		return True;
	}
	
	public function GetUserEmpRid($p_userRid){
		$this->db->select('_employeers.rid');
		$this->db->from('_users');
		$this->db->join('_employeers', '_users._employeers_rid = _employeers.rid AND _employeers.archive = 0');
		$this->db->where(array('_users.rid'=>$p_userRid, '_users.archive'=>0));
		$query = $this->db->get();
		if($query->num_rows) {
			$row = $query->row();
			return $row->rid;
		}
		return False;
	}
}
?>